<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.OutpurmatDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream" %>  
<%@page import="java.io.OutputStream" %>  
<%@page import="jxl.Workbook" %>  
<%@page import="jxl.format.Alignment" %> 
<%@page import="jxl.format.Border" %> 
<%@page import="jxl.format.BorderLineStyle" %>  
<%@page import="jxl.format.Colour" %>  
<%@page import="jxl.format.VerticalAlignment" %>  
<%@page import="jxl.write.Label" %>  
<%@page import="jxl.write.WritableCellFormat" %>  
<%@page import="jxl.write.WritableFont" %>  
<%@page import="jxl.write.WritableSheet" %>
<%@page import="jxl.write.WritableWorkbook" %> 
<%@page import="java.text.SimpleDateFormat"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	OutpurmatDAO outpurmatDao = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");

	String searchSub = request.getParameter("searchSub");
	String searchType = request.getParameter("searchType");
	String searchKeyword = request.getParameter("searchKeyword");
	String searchDate = request.getParameter("searchDate");
	String pnStr = request.getParameter("pn");
	String execl="";
	int pn = 0;
	int pageSize = 10;
	Integer teamId = (Integer)session.getAttribute("tid");
	
	int type = 1;
	
	if(pnStr != null && !"".equals(pnStr.trim())){
		try{
			pn = Integer.parseInt(pnStr);
		}catch(Exception e){
			pn = 0;
		}
	}
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "r.isDelete", 0});
	where.add(new Object[]{"eq", "o.teamId", teamId});
	where.add(new Object[]{"eq", "o.phase", 5});
	where.add(new Object[]{"eq", "state", 3});
	
	if(searchSub != null && !"".equals(searchSub)){
		if(!"0".equals(searchSub))
			where.add(new Object[]{"eq", "r.subStore", Integer.parseInt(searchSub)});
	}
	
	if(searchType != null && !"".equals(searchType)){
		if("1".equals(searchType))
			where.add(new Object[]{"like", "ou.name", searchKeyword});
		else if("2".equals(searchType))
			where.add(new Object[]{"like", "o.outCode", searchKeyword});
		else if("3".equals(searchType))
			where.add(new Object[]{"like", "rm.code", searchKeyword});
		else if("4".equals(searchType))
			where.add(new Object[]{"like", "rm.name", searchKeyword});
		else if("5".equals(searchType))
			where.add(new Object[]{"like", "rpl.name", searchKeyword});
	}
	
	if(searchDate != null && !"".equals(searchDate)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(searchDate);
		where.add(new Object[]{"eq", "o.outReqDate", dateVal});
	}
	
	Map map = outpurmatDao.getOutpurmat(0, 0,
			new String[][]{new String[]{"desc","date"}},
			where,
			new String[][]{new String[]{"","id"},new String[]{"","o.outReqDate"},new String[]{"","o.outCode"},new String[]{"","rm.code"},new String[]{"","rm.name"},new String[]{"","rm.company"},new String[]{"","outReqNum"},new String[]{"","r.purPrice"},new String[]{"","r.batch"},new String[]{"","rpl.name"},new String[]{"","rs.name"},new String[]{"","opr.code"},new String[]{"","opr.name"},new String[]{"","or.name"},new String[]{"","oc.name"},new String[]{"","ou.name"},new String[]{"","taskCode"}}		
			,new String[]{"outreq","receivepurmat","place","material","substore","project","company","reccompany","user"});
	
	request.setAttribute("map",map);
	execl = getExecl(request,map);
	int pageTotal = ((Integer)map.get("pageTotal")).intValue();
	int pageNo = ((Integer)map.get("pageNo")).intValue();
	List pageList=getPage(pageTotal,pageNo);
	request.setAttribute("pages",pageList);
	
	request.setAttribute("searchSub",searchSub);
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchKeyword",searchKeyword);
	request.setAttribute("searchDate",searchDate);
	request.setAttribute("xlsName",execl);
	request.setAttribute("xls",true);
	pageContext.forward("/vm/search/seahcOutStock.vm");
%>
<%!
private String getExecl(HttpServletRequest request,Map map) {
		///导出execl
		String sServerFileName="";
		String sLocalFileName = "";
		java.util.Date dt = new java.util.Date(System.currentTimeMillis());
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sServerFileName= "exScarch"+fmt.format(dt);
		String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
		String sPath=sWebRootPath+"exportTemp/";
		System.out.println("targetfile==============================================================》"+sPath);
		request.setAttribute("xlsName",sServerFileName+".xls");
		String execlName = sPath+sServerFileName+".xls";
		//sServerFileName =sServerFileName + sExt;
		//如果不存在该目录，则新建一个
		//File dir =new File(sPath);
		File dir =new File(sPath);
		if (!dir.exists()){
			dir.mkdirs();
		}
		String targetfile = execlName;// 输出的excel文件名  
        String worksheet = "check_"+sServerFileName;// 输出的excel文件工作表名
          
        String[] title = { "行号","日期","业务类型","出库单号","物资编码","物资名称","单 位","出库数量","单价","金额","批次","库位","子库存","项目编码","项目名称","领用单位","施工单位","申请人","任务编码"};// excel工作表的标题
       	WritableWorkbook workbook;
       
       	 try {  
            //System.out.println("begin");  
  
            OutputStream os = new FileOutputStream(targetfile);  
            workbook = Workbook.createWorkbook(os);  
            WritableSheet sheet = workbook.createSheet(worksheet, 0); // 添加第一个工作表   
            //设置内容格式  
            WritableCellFormat contentFormat = new WritableCellFormat();  
            contentFormat.setAlignment(Alignment.CENTRE);  
            Label label;  
            for (int i = 0; i < title.length; i++) {  
                // Label(列号,行号 ,内容 )  
                label = new jxl.write.Label(i, 0, title[i]); // put the title in  
                                                                // row1  
                label.setCellFormat(contentFormat);  
                sheet.addCell(label);  
            }
            if(map!=null){
            	List list  = (List)map.get("data");
            		//System.out.println("targetfile==============================================================》"+list.size());
            	if(list.size()>0){
            		for(int j=0;j<list.size();j++){
            			Object[] ex = (Object[])list.get(j);
            			//label = new jxl.write.Label(j, 0, ""+j);
            			//System.out.println("targetfile==============================================================》"+ex);
            			int line=0;
            			for(int k=0;k<ex.length;k++){
            				if(k==0){
            					label = new jxl.write.Label(k, j+1,j+1+"");
            					label.setCellFormat(contentFormat);  
                				sheet.addCell(label);
            				}else{
            					if(k==2){
            						label = new jxl.write.Label(k+line, j+1,"出库");
            						label.setCellFormat(contentFormat);  
                					sheet.addCell(label);
                					line=line+1;
            					}
            					if(k==8){
            						float dj=0;
            						float je=0f;
            						if(ex[6]!=null){
            							dj=Float.valueOf(ex[6].toString());
            						}
            						if(ex[7]!=null){
            							je = Float.valueOf(ex[7].toString());
            						}
            						float zj=dj*je;
            						label = new jxl.write.Label(k+line, j+1,String.valueOf(zj));
            						label.setCellFormat(contentFormat);  
                					sheet.addCell(label);
                					line=line+1;
            					}
            					if(ex[k]==null){
            						label = new jxl.write.Label(k+line, j+1,"");
            						label.setCellFormat(contentFormat);  
                					sheet.addCell(label);
            					}else{
            						label = new jxl.write.Label(k+line, j+1,ex[k].toString());
            						label.setCellFormat(contentFormat);  
                					sheet.addCell(label);
                				}
                				
            				}
            				
            			}
            		}	
            	}
            }
            workbook.write();  
            workbook.close();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }  
	return sServerFileName+".xls";
}
%>